package com.hengpeng.itfinoss.controller.userTypeApply;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.util.DateTimeUtil;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinbase.en.UserAppStatus;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.itfinbase.persist.UserInfo.RegionalType;
import com.hengpeng.itfinbase.persist.UserInfo.UserInfoType;
import com.hengpeng.itfinbase.persist.UserTypeApply;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.controller.util.PageHandlerUtil;
import com.hengpeng.itfinoss.controller.vo.UserTypeVo;
import com.hengpeng.itfinoss.service.ServiceFactory;

/**
 * 用户申请记录
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/userTypeApply")
public class UserTypeApplyController extends BaseController {
    @Autowired
    private ServiceFactory serviceFactory;

    private String jspPath(String pageName) {

        return "userTypeApply/" + pageName;
    }

    @RequestMapping(value = "/load/{id}")
    public String ossConfig(ModelMap map, HttpServletRequest request, @PathVariable Long id) {
        UserTypeApply userTypeApply = serviceFactory.getPersistService().get(UserTypeApply.class, id);

        UserInfo userInfo = serviceFactory.getPersistService().get(UserInfo.class, userTypeApply.getUserId());
        UserTypeVo user = new UserTypeVo();
        user.setId(userTypeApply.getId());
        user.setUserId(userTypeApply.getUserId());
        user.setUserName(userInfo.getUserName());
        user.setEmail(userInfo.getEmail());
        user.setRealName(userInfo.getRealName());
        user.setMobile(userInfo.getMobile());
        user.setType(userInfo.getType());
        user.setCreateTime(userTypeApply.getCreateTime());
        user.setUpdateTime(userTypeApply.getUpdateTime());
        user.setAppUserInfoType(userTypeApply.getAppUserInfoType());
        user.setUserApplyStatus(userTypeApply.getUserApplyStatus());
        user.setRegionalType(userTypeApply.getRegionalType());
        map.put("userTypeVo", user);
        return jspPath("/detail");
    }

    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request, ModelMap map) {

        Pagination page = new Pagination(request);
        page.setPageSize(this.getPageSize(request));
        try {
            ServiceResult<List<?>> result = serviceFactory.getUserTypeApplyService().selectUserTypeApplyPage(
                    page.currentPage, page.pageSize);
            page.setEntityCount(result.getReturnListSize());
            List<?> list = result.getReturnValue();
            List<UserTypeVo> userList = new ArrayList<UserTypeVo>();
            for (int i = 0; i < list.size(); i++) {
                UserTypeVo user = new UserTypeVo();
                Object[] obj = (Object[]) list.get(i);
                user.setId(Long.parseLong(String.valueOf(obj[0])));
                user.setRegionalType(RegionalType.valueOf(obj[7].toString()));
                user.setUserId(Long.parseLong(String.valueOf(obj[6])));
                user.setUserName(String.valueOf(obj[35]));
                user.setEmail(String.valueOf(obj[12]));
                user.setRealName(String.valueOf(obj[26]));
                user.setMobile(String.valueOf(obj[22]));
                user.setType(UserInfoType.valueOf(obj[33].toString()));
                user.setCreateTime(DateTimeUtil.formatToDate(String.valueOf(obj[3]), DateTimeUtil.DATE_FORMAT_YMDHMS));
                user.setUpdateTime(DateTimeUtil.formatToDate(String.valueOf(obj[4]), DateTimeUtil.DATE_FORMAT_YMDHMS));
                user.setAppUserInfoType(UserInfoType.valueOf(obj[2].toString()));
                user.setUserApplyStatus(UserAppStatus.valueOf(obj[5].toString()));
                userList.add(user);
            }
            map.put("userTypeApplyList", userList);
            map.put("userTypeApplyListPage", page);
            return "/userTypeApply/list";
        } catch (Exception ex) {
            ex.printStackTrace();
            return returnError(map, ex);
        }
    }

    @RequestMapping(value = "/approval")
    public String update(HttpServletRequest request, ModelMap map, @ModelAttribute UserTypeVo userTypeVo) {
        try {
            UserTypeApply modify = serviceFactory.getUserTypeApplyService().modify("PASS", userTypeVo.getId());
            serviceFactory.getUserInfoService().modify(userTypeVo.getAppUserInfoType().toString(), modify.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return PageHandlerUtil.returnError(map, e);
        }
        return PageHandlerUtil.returnSuccess(map, "/userTypeApply/list");
    }

    @RequestMapping(value = "/reject")
    public String update1(HttpServletRequest request, ModelMap map, @ModelAttribute UserTypeVo userTypeVo) {
        try {
            serviceFactory.getUserTypeApplyService().modify("UNPASS", userTypeVo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return PageHandlerUtil.returnError(map, e);
        }
        return PageHandlerUtil.returnSuccess(map, "/userTypeApply/list");
    }
}
